package com.twotiger.and.adapter;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

import android.app.Activity;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import app.twotiger.p2p.R;

import com.twotiger.and.Constants;
import com.twotiger.and.activity.WebViewPage;
import com.twotiger.and.activity.base.BaseActivity;
import com.twotiger.and.bean.AssertMatch;
import com.twotiger.and.util.ArithUtils;
import com.twotiger.and.util.ListUtils;
import com.twotiger.and.util.URLUtil;

public class CurrentAssertMatchAdapter extends BaseAdapter {

    private LayoutInflater minflater;
    private ArrayList<AssertMatch> mList;
    private Activity mActivity;

    public CurrentAssertMatchAdapter(Activity activity, ArrayList<AssertMatch> list) {
        super();
        minflater = LayoutInflater.from(activity);
        mList = list;
        mActivity = activity;
    }

    @Override
    public int getCount() {
        if (ListUtils.isEmpty(mList)) {
            return 1;
        }
        return mList.size();
    }

    @Override
    public Object getItem(int position) {
        return null;
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {

        if (convertView == null) {
            convertView = minflater.inflate(R.layout.current_assert_match_item, null);
        }
        TextView tv_project = ViewHolder.get(convertView, R.id.tv_project);
        TextView tv_amount = ViewHolder.get(convertView, R.id.tv_amount);
        ImageView iv_arrow = ViewHolder.get(convertView, R.id.iv_arrow);

        if (!ListUtils.isEmpty(mList)) {
            tv_project.setText(mList.get(position).getProjectName());
            tv_amount.setText(ArithUtils.coverMoneyComma(mList.get(position).getAmount() + ""));
            if (!"0".equals(mList.get(position).getInvestId())) {
                iv_arrow.setVisibility(View.VISIBLE);
            } else {
                iv_arrow.setVisibility(View.INVISIBLE);
            }
            convertView.setOnClickListener(new OnClickListener() {

                @Override
                public void onClick(View v) {

                    if (!"0".equals(mList.get(position).getInvestId())) {
                        Intent intent = new Intent(mActivity, WebViewPage.class);
                        intent.putExtra("title", "借款合同");
                        Map<String, String> contractmap = new HashMap<String, String>();
                        contractmap.put("pType", mList.get(position).getpType());
                        contractmap.put("investId", mList.get(position).getInvestId());
                        contractmap.put("token", ((BaseActivity) mActivity).getToken());
                        intent.putExtra(
                            "url",
                            Constants.HOST + Constants.CONTRACT + "contractHtml?"
                                    + URLUtil.createContractUrl(contractmap, "3.0"));
                        ((BaseActivity) mActivity).startNewActivity(intent, R.anim.push_left_in,
                            R.anim.push_left_out, false);
                    }
                }
            });
        }
        return convertView;
    }

    public void resetData(ArrayList rlist) {
        if (!rlist.isEmpty()) {
            mList.clear();
            mList.addAll(rlist);
            notifyDataSetChanged();
        }

    }

    public void addData(ArrayList alist) {
        if (!alist.isEmpty()) {
            mList.addAll(alist);
            notifyDataSetChanged();
        }
    }

}
